package com.cake.ssm.controller;

import com.cake.ssm.po.Activity;
import com.cake.ssm.service.ActivityService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by lijiawang1 on 2017/4/23.
 */
@SessionAttributes(value = {"filepath","allActivity","photoFilePath"})
@Controller
public class ActivityController {

    @Resource
    private ActivityService activityService;

    @RequestMapping("/RedriectUpLoadPhoto")
    public String RedriectPublishActivity()
    {
        return "PublishActivity";
    }
    @RequestMapping("/UpLoadPhoto")
    public String RedriectUpLoadPhoto()
    {
        return "UpLoadPhoto";
    }

    @RequestMapping("/InsertActivity")
    public String PublishActivity(Activity activity) throws Exception
    {

        UUID uuid=UUID.randomUUID();
        activity.setActivityid(uuid.toString());
        System.out.println(activity);
        activityService.InsertActivity(activity);
        return "success";
    }
    @RequestMapping("/SelectAllActivity")
    public String SelectAllActivity(Map<String,Object> map,HttpServletRequest request)throws Exception
    {
        List<Activity> list=activityService.SelectAllActivity();
        String photoFilePath=request.getSession().getServletContext().getRealPath("/photo");
        System.out.println("photoFilePath==="+photoFilePath);
        map.put("allActivity",list);
        map.put("photoFilePath",photoFilePath);
        return list!=null?"ShowAllActivity":"error";
    }



    @RequestMapping("/photoUpload")
    public String  fileUpload(@RequestParam("file1")MultipartFile file,
                             Map<String,Object> map,HttpServletRequest request) throws IOException {
        if(file!=null) {
            String path = null;// 文件路径
            String type = null;// 文件类型
            String fileName = file.getOriginalFilename();
           // System.out.println("上传的文件原名称:" + fileName);

            type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
             if (type != null) {
                   if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase()) || "JPG".equals(type.toUpperCase())){
                       String realPath=request.getSession().getServletContext().getRealPath("/photo");

                       System.out.println("realPath="+realPath);
                       path=realPath+"\\"+fileName;
                       System.out.println("path="+path);

                       File uploadFile=new File(path);
                       if(!uploadFile.exists()){
                           uploadFile.mkdirs();
                       }
                       map.put("filepath",path);
                      // request.getSession().setAttribute("filepath",path);
                       file.transferTo(uploadFile);
                   }else {
                        System.out.println("不是我们想要的文件类型,请按要求重新上传");
                   }
              }else{
                 request.getSession().setAttribute("TypeError","文件类型不想符合要求");
                 System.out.println("文件类型不想符合要求");
             }
        }else{
            request.getSession().setAttribute("FileError","没有找到相对应的文件");
            System.out.println("没有找到相对应的文件");
        }
        return "PublishActivity";
    }
}
